﻿namespace WcfComposition.Tests
{
    using System;
    using System.Text;
    using System.Collections.Generic;
    using System.Linq;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using System.ComponentModel.Composition.Hosting;

    /// <summary>
    /// Class responsible for unit testing the <see cref="ComposingInstanceProvider"/> class.
    /// </summary>
    [TestClass]
    public class ComposingInstanceProviderTests
    {
        #region Methods

        #region Setup

        private CompositionContainer GetContainer()
        {
            var catalog = new TypeCatalog( typeof( FakeService ) );
            var container = new CompositionContainer( catalog );

            return container;
        }

        #endregion

        [TestMethod]
        public void ContainerShouldNotReturnNull()
        {
            var container = this.GetContainer();
            var provider = new ComposingInstanceProvider( typeof( object ), container );

            Assert.AreSame( container, provider.Container );
        }

        [TestMethod]
        public void TypeShouldNotBeNull()
        {
            var type = typeof( object );
            var provider = new ComposingInstanceProvider( type, new CompositionContainer() );

            Assert.AreSame( type, provider.ServiceType );
        }

        [TestMethod]
        public void ConstructorShouldThrowExceptionIfNullIsPassedForType()
        {
            var errored = false;

            try
            {
                var provider = new ComposingInstanceProvider( null, this.GetContainer() );
            }
            catch ( ArgumentNullException ex )
            {
                errored = true;
            }

            Assert.IsTrue( errored );
        }

        [TestMethod]
        public void ConstructorShouldThrowExceptionIfNullIsPassedForContainer()
        {
            var errored = false;

            try
            {
                var provider = new ComposingInstanceProvider( typeof(object), null );
            }
            catch ( ArgumentNullException ex )
            {
                errored = true;
            }

            Assert.IsTrue( errored );
        }

        [TestMethod]
        public void GetInstanceByContextAndMessageShouldReturn()
        {
            var provider = new ComposingInstanceProvider( typeof( FakeService ), this.GetContainer() );

            var result = provider.GetInstance( null, null );

            Assert.IsInstanceOfType( result, typeof( FakeService ) );
        }

        [TestMethod]
        public void GetInstanceByContextShouldReturn()
        {
            var provider = new ComposingInstanceProvider( typeof( FakeService ), this.GetContainer() );

            var result = provider.GetInstance( null );

            Assert.IsInstanceOfType( result, typeof( FakeService ) );
        }

        #endregion
    }
}
